package com.example.demo.controller;

import com.example.demo.entity.Scenery;
import com.example.demo.service.SceneryService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.github.pagehelper.PageInfo;

import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/scenery")
public class SceneryController {
    SceneryService sceneryService;

    @GetMapping("/getAll")
    public List<Scenery> getAll() {
        List<Scenery> list = sceneryService.getAll();
        return list;
    }

    @PostMapping(value = "/add",consumes = MediaType.MULTIPART_FORM_DATA_VALUE,produces = "application/json; charset=utf-8")
    public String addScenery(@RequestParam(value = "image")MultipartFile image,Scenery scenery) throws IOException {

        System.out.println(image);
        //获取原始文件名
        String oldFileName = image.getOriginalFilename() ;
        //获取文件后缀名
        String suffix = oldFileName.substring(oldFileName.lastIndexOf(".")) ;
        //文件上传之后的文件名
        String fileName = UUID.randomUUID().toString() + suffix ;
        //文件上传之后的全路径
        String path = "F:\\java代码\\mall_image\\" + fileName ;
        //创建一个新文件
       File newFile = new File(path) ;
        //复制文件
        FileUtils.copyInputStreamToFile(image.getInputStream(),newFile);
        image.transferTo(newFile);
        scenery.setSceneryImage(fileName);
        int r = sceneryService.addScenery(scenery);
        if (r > 0) {

            return "ok";
        } else {
           return "error";
        }
    }
    @GetMapping("/delete")
    public String delete(Integer sceneryId){
       int r= sceneryService.deleteScenery(sceneryId);
        if(r>0){
            return  "ok";
        }else{
            return "no";
        }
    }

    @GetMapping("/update")
    public String update(Scenery scenery){
        int r=sceneryService.updateScenery(scenery);
        if(r>0){
            return  "ok";
        }else{
            return "no";
        }
    }
    @GetMapping("/findId")
    public Scenery findSceneryById(Integer sceneryId){
        Scenery scenery=sceneryService.findSceneryById(sceneryId);
        return scenery;
    }

    @GetMapping("/find")
    public  PageInfo<Scenery> find(String sceneryCity, String sceneryTitle, Double lowPrice, Double highPrice,@RequestParam("pageNo") Integer pageNo,@RequestParam(defaultValue = "2") int pageSize){
        System.out.println(sceneryCity+"="+sceneryTitle+"="+lowPrice+"="+highPrice);
        PageInfo<Scenery> list = sceneryService.find(sceneryCity,sceneryTitle,lowPrice,highPrice,pageNo,pageSize) ;
        return list ;
    }

    //分页
    @GetMapping("/page")
    public PageInfo<Scenery> page(@RequestParam("pageNo") Integer pageNo,@RequestParam(defaultValue = "3") int pageSize){
        System.out.println(pageNo+"=========");
        PageInfo<Scenery> pageInfo = sceneryService.getPage(pageNo,pageSize);
        return pageInfo ;
    }



    @Autowired
    public void setSceneryService(SceneryService sceneryService) {
        this.sceneryService = sceneryService;
    }
}
